﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BYRBBS.Model;
using BYRBBS.Util;
using System.Diagnostics;
using System.Xml.Linq;
using BYRBBS.MyDebug;
using Microsoft.Phone.Controls;

namespace BYRForum
{
    public partial class ManageThreadItem : UserControl
    {
        ThreadsDetailPage basePage;
        public ManageThreadItem(PhoneApplicationPage parent)
        {
            InitializeComponent();
            basePage = (ThreadsDetailPage)parent;
            
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定删除帖子？", "删除", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            string url = ByrApi.apiUrl + "/article/" + App.ArticleList.Items[App.ArticleItemChoose].Article_Item.Board_name +
                "/delete" + "/" + App.ArticleList.Items[App.ArticleItemChoose].Article_Item.Id + ByrApi.Encode + ByrApi.appkey;
            HttpUtil.HttpPost(url, Delete_Callback);
            this.CloseMeAsPopup();
        }

        private void Delete_Callback(IAsyncResult asynchronousResult)
        {
            try
            {
                HttpWebRequest hwrq = (HttpWebRequest)asynchronousResult.AsyncState;
                hwrq.BeginGetResponse(DeleteResponse_Callback, hwrq);
            }
            catch (Exception e)
            {
                Debug.WriteLine("POST STRING FAILED:" + e.Message);
            }
        }

        private void DeleteResponse_Callback(IAsyncResult asynchronousResult)
        {
            try
            {
                string result = HttpUtil.ReadToEnd(asynchronousResult);
                XDocument doc = XDocument.Parse(result);
                DebugInfo.writeXml(result);
                foreach (XElement item in doc.Descendants("error").Nodes())
                {
                    if (item.Name == "msg")
                    {
                        this.Dispatcher.BeginInvoke(
                            () =>
                            {
                                MessageBox.Show("删除失败：" + item.Value);
                            }
                            );

                        return;
                    }
                }
                this.Dispatcher.BeginInvoke(
                    () =>
                    {
                        MessageBox.Show("删除成功！");
                        string url = ByrApi.apiUrl + "/threads/" + App.TempThreadItem.BoardName + "/" +
                         App.TempThreadItem.GroupID + ByrApi.Encode + ByrApi.appkey + "page=" + (basePage.current_page).ToString();
                        basePage.GoToPage(basePage.current_page);
                        
                    }
                    );



            }
            catch { }

        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            this.CloseMeAsPopup();
            basePage.NavigationService.Navigate(new Uri("/BYRForum;component/EditArticle.xaml", UriKind.Relative));
        }
    }
}
